<?php

    /**
     * Tecdoc
     *
     * @package      Core
     * @subpackage   Svn_Adapter
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
	class Core_Svn_Adapter_Svn extends Core_Svn_Adapter_Abstract
	{

	////////////////////////////////////////////////////////////////////////////
	//                                 Methods                                //
	////////////////////////////////////////////////////////////////////////////

        /**
         * Add
         *
         * @return      string
         */
        public function add($files = '*')
        {
            return $this->_execute('cd '. $this->_destination .' && '. $this->_binary .' add --depth=empty --username='.  $this->_login .' --password='. $this->_password .' '. $files);
        }

        /**
         * Checkout project
         *
         * @return      string
         */
        public function checkout()
        {
            return $this->_execute('cd '. $this->_destination .' && '. $this->_binary .' checkout --username='.  $this->_login .' --password='. $this->_password .' '. $this->_source .' . ');
        }

        /**
         * Updates project
         *
         * @return      void
         */
        public function update()
        {
            return $this->_execute('cd '. $this->_destination .' && '. $this->_binary .' update --username='.  $this->_login .' --password='. $this->_password .' '. $this->_destination);
        }

        /**
         * Commits project
         *
         * @param       string      $comment
         * @return      string
         */
        public function commit($comment = 'Commit from Serum')
        {
            return $this->_execute($this->_binary .' add '. $this->_destination .'/* && cd '. WWW_PATH .' && '. $this->_binary .' commit --username='.  $this->_login .' --password='. $this->_password .' '. $this->_destination .' --mesage "'. $comment .'"');
        }

        /**
         * Gets status
         *
         * @return      string
         */
        public function status()
        {
            return $this->_execute($this->_binary .' status --username='.  $this->_login .' --password='. $this->_password .' '. $this->_destination);
        }

        /**
         * Ignores
         *
         * @param       string      $content
         * @param       string      $path
         * @param       boolean     $recursive
         * @return      string
         */
        public function ignore($content, $path = '.', $recursive = false)
        {
            return $this->_execute('cd '. $this->_destination .' && '. $this->_binary .' '. (($recursive) ? '-R' : '') . ' propset --username='.  $this->_login .' --password='. $this->_password .' svn:ignore "'. $content .'" '. $path);
        }

        /**
         * Executes a system command
         *
         * @param       string      $cmd
         * @param       string      $function
         * @param       boolean     $log
         * @return      string
         */
        protected function _execute($cmd)
        {
            $return = $code = null;

            /*
             * Executes
             */
            exec($cmd . ' 2>&1', &$return, &$code);

            if ($code != 0) {
                if (empty($return)) {
                    throw new Core_Exception('Fatal error : An error occured while execute svn command');
                }
                
                $errors = '';
                
                foreach ((array) $return as $error) {
                    $errors .= $error ." ; ";
                }

                throw new Core_Exception($errors);
            }

            return $return;
        }
	}
